package util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.commons.io.FileUtils;

public class CopyInternalFiles
{
	public static void copyFile(String internalPath, String outPath)
	{
	    InputStream is = null;
	    BufferedReader br = null;
	    String line;
	    ArrayList<String> list = new ArrayList<String>();

	    try { 
	      is = FileUtils.class.getResourceAsStream(internalPath);
	      br = new BufferedReader(new InputStreamReader(is));
	      while (null != (line = br.readLine())) {
	         list.add(line);
	      }
	    }
	    catch (Exception e) {
	      e.printStackTrace();
	    }
	    finally {
	      try {
	        if (br != null) br.close();
	        if (is != null) is.close();
	      }
	      catch (IOException e) {
	        e.printStackTrace();
	      }
	    }
	    
	    //Output
	    try {
			FileUtils.writeLines(new File(outPath), list);
		} catch (IOException e) {
			System.out.println("Error copying files from inside jar");
			e.printStackTrace();
			//System.exit(1);
		}
	}
	
	public static void copyConfigFilesToDirectory(String fileName1, String fileName2) {
		try {
			
			// create dir if doesn't exist
			File tmp = new File(fileName1);
			tmp.getParentFile().mkdirs();
			tmp = new File(fileName2);
			tmp.getParentFile().mkdirs();
			
			String conf1String = "###This is the config file for pp2features that tells which features should be extracted.\n" + 
					"###The syntax is as follows:\n" + 
					"###Comments are introduced by # and are ignored, as are empty lines.\n" + 
					"###There are blocks, each corresponding to a specific part of predict protein, e.g. blast, prof, md, etc\n" + 
					"###Each block offers several features and options that are specific to that block.\n" + 
					"###By setting a 1 or 0 the user tells pp2features which feature she/he wants to include into the final output file.\n" + 
					"###Note, that window length has to be uneven!\n" + 
					"\n" + 
					"#####################################\n" + 
					"#Protein identifier\n" + 
					"prot_id			1	#Should the protein identifier (equal to the pp directory name) _plus_ the sequence position be prepended to each instance in the output file? \n" + 
					"				#Useful for homology-based train-/testset splitting (not provided by pp2features!)\n" + 
					"\n" + 
					"#####################################\n" + 
					"#Some global protein features, hence no window here to be set\n" + 
					"#glbl_aa_comp		1	#The global amino acid composition, i.e. a 20-values feature\n" + 
					"#glbl_length		1	#The protein's sequence length, cumulatively categorized into 4 bins, i.e 4-value feature\n" + 
					"glbl_sec		1	#The overall secondary structure, each of H,E,L cumulatively categorized into 3 bins, i.e. an overall 9-value feature\n" + 
					"glbl_acc		1	#The overall solvent accessibility, each of e,i,b cumulatively categorized into 3 bins, as in glbl_sec\n" + 
					"\n" + 
					"#####################################\n" + 
					"#Chemical properties, everything normalized to [0,1]\n" + 
					"#chemprop_mass		1	#Mass of the residue\n" + 
					"#chemprop_vol		1	#Volume\n" + 
					"#chemprop_hyd		1	#Hydropathy\n" + 
					"#chemprop_cbeta		1	#Cbeta branching\n" + 
					"#chemprop_hbreak		1	#Helix breaker, i.e. proline\n" + 
					"#chemprop_charge		1	#Charge\n" + 
					"#chemprop_window		33	#Window length around the central residue\n" + 
					"\n" + 
					"#####################################\n" + 
					"#Raw Sequence\n" + 
					"#raw_seq			0	#Like blast's pssm matrix, however using a 1 at the specific column, 0 otherwise. Useful to examine the improvement of pssm over sequence only\n" + 
					"#raw_seq_window		33	#Window length around central residue\n" + 
					"\n" + 
					"#####################################\n" + 
					"#Blast\n" + 
					"pssm			1	#The first part in blast's output, i.e. the actual pssm matrix\n" + 
					"#perc			1	#The second part, i.e. the percentage matrix\n" + 
					"#inf_PP	 		1	#The second last column in blast's output matrix, i.e. the information per position\n" + 
					"#rel_weight		1	#The very last column in blast's output matrix\n" + 
					"blast_normalize		1	#Should the values be normalized to fit into [0,1]? pssm (logistically), perc (linearly), inf_PP (logistically), rel_weight (logistically)\n" + 
					"blast_window		21	#Window length around central residue\n" + 
					"\n" + 
					"#####################################\n" + 
					"#PSIC\n" + 
					"#Position-specific independent counts, s. S.Sunyaev et al, 1999, Protein Engineering, vol12, no5, p387-394\n" + 
					"#psic			1	#The psic matrix\n" + 
					"#num_seq			1	#Number of aligned residues per position\n" + 
					"#psic_normalize		1	#Normalize psic (logistically, as in pssm) and num_seq (linearly) to fit both in [0,1]\n" + 
					"#psic_window		33	#Window length around central residue\n" + 
					"\n" + 
					"#####################################\n" + 
					"#DISIS\n" + 
					"#Protein-DNA interaction sites\n" + 
					"disis_bin		1	#Binary (i.e. 2-state) prediction of disis, split into two separate features\n" + 
					"#disis_raw		1	#Raw (numeric) prediction\n" + 
					"disis_normalize		1	#Normalize those values linearly to fit in [0,1] (otherwise between -100 and 100)\n" + 
					"disis_window		5	#Window length around central residue\n" + 
					"\n" + 
					"#####################################\n" + 
					"#ISIS\n" + 
					"#PPI sites\n" + 
					"#isis_bin		1	#Binary (i.e. 2-state) prediction of isis, split into two separate features\n" + 
					"#isis_raw		1	#Raw (numeric) prediction\n" + 
					"#isis_normalize		1	#Normalize those values linearly to fit in [0,1] (otherwise between -100 and 100)\n" + 
					"#isis_window		5	#Window length around central residue\n" + 
					"\n" + 
					"#####################################\n" + 
					"#PROFbval\n" + 
					"#Residue flexibility\n" + 
					"#Note, that PROFbval does not output a binary prediction in the pp version, hence we deal here with the raw values only!\n" + 
					"#profbval_raw1		1	#Output from node1 of bval's NN\n" + 
					"#profbval_raw2		1	#Output from node2 of bval's NN\n" + 
					"#profbval_normalize	1	#Normalize both linearly to fit [0,1]? Otherwise [0,100]\n" + 
					"#profbval_window		33	#Window length around central residue\n" + 
					"\n" + 
					"#####################################\n" + 
					"#MD\n" + 
					"#Meta disorder predictor\n" + 
					"#md_bin			1	#Binary (i.e. 2-state) prediction of md, split into two separate features\n" + 
					"#md_raw			1	#Raw (numeric) prediction, already in [0,1]\n" + 
					"#md_ri			1	#Reliability index, between 0-9\n" + 
					"#md_normalize		1	#Normalize the ri linearly to [0,1], md_raw already is\n" + 
					"#md_window		5	#Window length around central residue\n" + 
					"\n" + 
					"#####################################\n" + 
					"#PROFPhd\n" + 
					"#secondary structure and solvent accessibility prediction\n" + 
					"#profphd_sec_bin		1	#Binary (i.e. 2-state) prediction of sec strct, split into three separate features, i.e. helix, strand, loop (PHEL column in prof's output)\n" + 
					"#profphd_sec_raw		1	#Raw (numeric) output from the network (OtH, OtE, OtL columns)\n" + 
					"#profphd_sec_ri		1	#Reliability index for sec strct prdct (RI_S column)\n" + 
					"#profphd_acc_bin		1	#Binary (i.e. 2-state) prediction of solvent accessibility, split into three separate features, i.e. e(exposed), i(ntermediate), b(urried). (Pbie column)\n" + 
					"#profphd_acc_rel		1	#Relative solvent accessibility (numeric, PREL column)\n" + 
					"#profphd_acc_ri		1	#Reliability index for solv acc prdct (RI_A column)\n" + 
					"#profphd_normalize	1	#Normalize profphd_sec_raw, profphd_sec_ri, profphd_acc_rel, profphd_acc_ri linearly to fit [0,1]?\n" + 
					"#profphd_window		33	#Window length around central residue\n" + 
					"\n" + 
					"#####################################\n" + 
					"#PfamA\n" + 
					"#This essentially featurizes the hmmer3 output on a per-residue basis.\n" + 
					"#It takes the three lines in the alignment beneath the model sequence and turns that into the 4 window-based features below. \n" + 
					"#pfam_within_domain	1	#Binary assignment whether the actual residue belongs to at least one domain\n" + 
					"#pfam_dom_cons		1	#How conserved is the residue's position within the domain family? 3-state assignment\n" + 
					"#pfam_residue_fit	1	#How well does the actual residue fit into the domain? 4-state assignment\n" + 
					"#pfam_pp			1	#The posterior probability that the residue actually matches its position in the domain\n" + 
					"#pfam_window		5	#Window length around central residue\n" + 
					"\n" + 
					"#####################################\n" + 
					"#Prosite patterns\n" + 
					"#This simply annotates a residue as 1 if being part of any prosite pattern, else 0.\n" + 
					"#prosite_part		1	#Binary assignment if part of any prosite match or not\n" + 
					"#prosite_window		5	#Window length around central residue\n" + 
					"";
			
			String conf2String = "###This is the config file for pp2features that tells which features should be extracted.\n" + 
					"###The syntax is as follows:\n" + 
					"###Comments are introduced by # and are ignored, as are empty lines.\n" + 
					"###There are blocks, each corresponding to a specific part of predict protein, e.g. blast, prof, md, etc\n" + 
					"###Each block offers several features and options that are specific to that block.\n" + 
					"###By setting a 1 or 0 the user tells pp2features which feature she/he wants to include into the final output file.\n" + 
					"###Note, that window length has to be uneven!\n" + 
					"\n" + 
					"#####################################\n" + 
					"#Protein identifier\n" + 
					"prot_id			1	#Should the protein identifier (equal to the pp directory name) _plus_ the sequence position be prepended to each instance in the output file? \n" + 
					"				#Useful for homology-based train-/testset splitting (not provided by pp2features!)\n" + 
					"\n" + 
					"#####################################\n" + 
					"#Some global protein features, hence no window here to be set\n" + 
					"#glbl_aa_comp		1	#The global amino acid composition, i.e. a 20-values feature\n" + 
					"#glbl_length		1	#The protein's sequence length, cumulatively categorized into 4 bins, i.e 4-value feature\n" + 
					"glbl_sec		1	#The overall secondary structure, each of H,E,L cumulatively categorized into 3 bins, i.e. an overall 9-value feature\n" + 
					"glbl_acc		1	#The overall solvent accessibility, each of e,i,b cumulatively categorized into 3 bins, as in glbl_sec\n" + 
					"\n" + 
					"#####################################\n" + 
					"#Chemical properties, everything normalized to [0,1]\n" + 
					"chemprop_mass		1	#Mass of the residue\n" + 
					"chemprop_vol		1	#Volume\n" + 
					"chemprop_hyd		1	#Hydropathy\n" + 
					"chemprop_cbeta		1	#Cbeta branching\n" + 
					"chemprop_hbreak		1	#Helix breaker, i.e. proline\n" + 
					"chemprop_charge		1	#Charge\n" + 
					"chemprop_window		33	#Window length around the central residue\n" + 
					"\n" + 
					"#####################################\n" + 
					"#Raw Sequence\n" + 
					"raw_seq			0	#Like blast's pssm matrix, however using a 1 at the specific column, 0 otherwise. Useful to examine the improvement of pssm over sequence only\n" + 
					"raw_seq_window		33	#Window length around central residue\n" + 
					"\n" + 
					"#####################################\n" + 
					"#Blast\n" + 
					"pssm			1	#The first part in blast's output, i.e. the actual pssm matrix\n" + 
					"perc			1	#The second part, i.e. the percentage matrix\n" + 
					"inf_PP	 		1	#The second last column in blast's output matrix, i.e. the information per position\n" + 
					"rel_weight		1	#The very last column in blast's output matrix\n" + 
					"blast_normalize		1	#Should the values be normalized to fit into [0,1]? pssm (logistically), perc (linearly), inf_PP (logistically), rel_weight (logistically)\n" + 
					"blast_window		33	#Window length around central residue\n" + 
					"\n" + 
					"#####################################\n" + 
					"#PSIC\n" + 
					"#Position-specific independent counts, s. S.Sunyaev et al, 1999, Protein Engineering, vol12, no5, p387-394\n" + 
					"psic			1	#The psic matrix\n" + 
					"num_seq			1	#Number of aligned residues per position\n" + 
					"psic_normalize		1	#Normalize psic (logistically, as in pssm) and num_seq (linearly) to fit both in [0,1]\n" + 
					"psic_window		33	#Window length around central residue\n" + 
					"\n" + 
					"#####################################\n" + 
					"#DISIS\n" + 
					"#Protein-DNA interaction sites\n" + 
					"disis_bin		1	#Binary (i.e. 2-state) prediction of disis, split into two separate features\n" + 
					"disis_raw		1	#Raw (numeric) prediction\n" + 
					"disis_normalize		1	#Normalize those values linearly to fit in [0,1] (otherwise between -100 and 100)\n" + 
					"disis_window		5	#Window length around central residue\n" + 
					"\n" + 
					"#####################################\n" + 
					"#ISIS\n" + 
					"#PPI sites\n" + 
					"isis_bin		1	#Binary (i.e. 2-state) prediction of isis, split into two separate features\n" + 
					"isis_raw		1	#Raw (numeric) prediction\n" + 
					"isis_normalize		1	#Normalize those values linearly to fit in [0,1] (otherwise between -100 and 100)\n" + 
					"isis_window		5	#Window length around central residue\n" + 
					"\n" + 
					"#####################################\n" + 
					"#PROFbval\n" + 
					"#Residue flexibility\n" + 
					"#Note, that PROFbval does not output a binary prediction in the pp version, hence we deal here with the raw values only!\n" + 
					"profbval_raw1		1	#Output from node1 of bval's NN\n" + 
					"profbval_raw2		1	#Output from node2 of bval's NN\n" + 
					"profbval_normalize	1	#Normalize both linearly to fit [0,1]? Otherwise [0,100]\n" + 
					"profbval_window		33	#Window length around central residue\n" + 
					"\n" + 
					"#####################################\n" + 
					"#MD\n" + 
					"#Meta disorder predictor\n" + 
					"md_bin			1	#Binary (i.e. 2-state) prediction of md, split into two separate features\n" + 
					"md_raw			1	#Raw (numeric) prediction, already in [0,1]\n" + 
					"md_ri			1	#Reliability index, between 0-9\n" + 
					"md_normalize		1	#Normalize the ri linearly to [0,1], md_raw already is\n" + 
					"md_window		5	#Window length around central residue\n" + 
					"\n" + 
					"#####################################\n" + 
					"#PROFPhd\n" + 
					"#secondary structure and solvent accessibility prediction\n" + 
					"profphd_sec_bin		1	#Binary (i.e. 2-state) prediction of sec strct, split into three separate features, i.e. helix, strand, loop (PHEL column in prof's output)\n" + 
					"profphd_sec_raw		1	#Raw (numeric) output from the network (OtH, OtE, OtL columns)\n" + 
					"profphd_sec_ri		1	#Reliability index for sec strct prdct (RI_S column)\n" + 
					"profphd_acc_bin		1	#Binary (i.e. 2-state) prediction of solvent accessibility, split into three separate features, i.e. e(exposed), i(ntermediate), b(urried). (Pbie column)\n" + 
					"profphd_acc_rel		1	#Relative solvent accessibility (numeric, PREL column)\n" + 
					"profphd_acc_ri		1	#Reliability index for solv acc prdct (RI_A column)\n" + 
					"profphd_normalize	1	#Normalize profphd_sec_raw, profphd_sec_ri, profphd_acc_rel, profphd_acc_ri linearly to fit [0,1]?\n" + 
					"profphd_window		33	#Window length around central residue\n" + 
					"\n" + 
					"#####################################\n" + 
					"#PfamA\n" + 
					"#This essentially featurizes the hmmer3 output on a per-residue basis.\n" + 
					"#It takes the three lines in the alignment beneath the model sequence and turns that into the 4 window-based features below. \n" + 
					"pfam_within_domain	1	#Binary assignment whether the actual residue belongs to at least one domain\n" + 
					"pfam_dom_cons		1	#How conserved is the residue's position within the domain family? 3-state assignment\n" + 
					"#pfam_residue_fit	1	#How well does the actual residue fit into the domain? 4-state assignment\n" + 
					"#pfam_pp			1	#The posterior probability that the residue actually matches its position in the domain\n" + 
					"pfam_window		5	#Window length around central residue\n" + 
					"\n" + 
					"#####################################\n" + 
					"#Prosite patterns\n" + 
					"#This simply annotates a residue as 1 if being part of any prosite pattern, else 0.\n" + 
					"prosite_part		1	#Binary assignment if part of any prosite match or not\n" + 
					"prosite_window		5	#Window length around central residue\n" + 
					"";
			
			
				BufferedWriter out = new BufferedWriter(new FileWriter(fileName1));
				out.write(conf1String);
				out.close();
				
				BufferedWriter out2 = new BufferedWriter(new FileWriter(fileName2));
				out2.write(conf2String);
				out2.close();
			
			}
			catch (IOException e)
			{
				System.err.println("Couldn't create configs:"+e.getMessage());
				e.printStackTrace();

			}
	}
}